package com.lishe.lishe.lishe.module.me.fragments;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.base.BaseFragment;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.common.WholeListView;
import com.lishe.lishe.lishe.module.business.activity.BusinessActivity;
import com.lishe.lishe.lishe.module.main.activity.MainActivity0;
import com.lishe.lishe.lishe.module.me.activity.AboutActivity;
import com.lishe.lishe.lishe.module.me.activity.AboutUpdateActivity;
import com.lishe.lishe.lishe.module.me.activity.ActivitySetting;
import com.lishe.lishe.lishe.module.me.activity.AddressManageActivity;
import com.lishe.lishe.lishe.module.me.activity.AdviceActivity;
import com.lishe.lishe.lishe.module.me.activity.MyAccountActivity;
import com.lishe.lishe.lishe.module.me.activity.MyBenefitsActivity;
import com.lishe.lishe.lishe.module.me.adapter.MineAdapter;
import com.lishe.lishe.lishe.module.me.bean.OrderType;
import com.lishe.lishe.lishe.module.me.bean.UserInfoObject;
import com.lishe.lishe.lishe.module.me.bean.UserListConfig;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.module.myorder.activity.MyOrdersActivity;
import com.lishe.lishe.lishe.module.recharge.activity.RechargeIntegralActivity;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.widget.CircleImageView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lwa on 2017/8/9.
 * 我的
 */

public class MineFragment extends BaseFragment implements WholeListView.OnItemClickListener, View.OnClickListener {
    private String TAG = getClass().getSimpleName();
    private List<UserListConfig> title_list;
    private static int kindofUser;
    private int[] ImageArray = new int[]{R.mipmap.qiye_icon, R.mipmap.money_icon, R.mipmap.gift_icon, R.mipmap.address_icon, R.mipmap.yijian_icon, R.mipmap.lishe_icon};
    private WholeListView showlist;
    private MineAdapter adapter;
    private TextView lab1, lab2, lab3, lab4, lab5;
    private ImageView recharge, Setting;
    private RelativeLayout order_waiting_pay, order_waiting_post, order_waiting_recieved, order_completion, order_cancel;
    private String companyName;
    private RelativeLayout relativeLayout;
//    MyBroadcastReceiver mBroadcastReceiver;

    TextView username;
    TextView totalPoints;
    TextView commonPoints;
    TextView limitPoints;
    TextView ninhao;
    CircleImageView headIcon;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, MineFragment.class);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_mine, null);
        initView(view);
        setListener();
        IntentFilter filter = new IntentFilter();
        filter.addAction("REFRESH");
        title_list = new ArrayList<>();
        adapter = new MineAdapter(getActivity(), title_list);
        showlist.setAdapter(adapter);
        view.setOnTouchListener(this);
        return view;
    }

    protected void initView(View v) {
        showlist = v.findViewById(R.id.listView);
        showlist.setOnItemClickListener(this);
        lab1 = v.findViewById(R.id.lab1);
        lab2 = v.findViewById(R.id.lab2);
        lab3 = v.findViewById(R.id.lab3);
        lab4 = v.findViewById(R.id.lab4);
        lab5 = v.findViewById(R.id.lab5);

        order_waiting_pay = v.findViewById(R.id.order_wait_pay);
        order_waiting_recieved = v.findViewById(R.id.order_wait_recieved);
        order_waiting_post = v.findViewById(R.id.order_wait_post);
        order_completion = v.findViewById(R.id.order_complete);
        order_cancel = v.findViewById(R.id.order_back);
        relativeLayout = v.findViewById(R.id.all_type_orders);
        Setting = v.findViewById(R.id.setting);
        recharge = v.findViewById(R.id.money);

        username = v.findViewById(R.id.user_name);
        totalPoints = v.findViewById(R.id.total_points);
        commonPoints = v.findViewById(R.id.common_points);
        limitPoints = v.findViewById(R.id.limit_points);
        ninhao = v.findViewById(R.id.ninhao);
        headIcon = v.findViewById(R.id.head_icon);

    }

    private void setListener() {
        relativeLayout.setOnClickListener(this);
        order_cancel.setOnClickListener(this);
        order_waiting_post.setOnClickListener(this);
        order_waiting_pay.setOnClickListener(this);
        order_waiting_recieved.setOnClickListener(this);
        order_completion.setOnClickListener(this);
        order_cancel.setOnClickListener(this);
        Setting.setOnClickListener(this);
        recharge.setOnClickListener(this);
    }


    LSWebHelper.WebCallBack response = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {

            LoadingUtils.closeDialog();
            JSONObject userConfigs = dataJson;
            String userInfo = userConfigs.optString("userInfo");
            UserInfoObject user = JSON.parseObject(userInfo, UserInfoObject.class);
            setUserInfo(user);
            String orderConfigs = userConfigs.optString("order_list_config");
            List<OrderType> list1 = JSON.parseArray(orderConfigs, OrderType.class);
            if (list1 != null && list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    OrderType order = list1.get(i);
                    setOrderData(order);
                }
            }
            //获取显示的列表 我的资产等
            {
                String userinfo_list_config = userConfigs.optString("userinfo_list_config");
                title_list = JSON.parseArray(userinfo_list_config, UserListConfig.class);
                //屏蔽我的福利
                title_list.remove(2);
                //屏蔽企业圈
                title_list.remove(0);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.resetData(title_list);
                    }
                });
            }

        }

        @Override
        public void onFailure(String message) {

            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            LoadingUtils.closeDialog();
        }
    };

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ListAdapter adapter = showlist.getAdapter();
        //通过 每一个item 获得 相对应 的 listsbean
        UserListConfig ob = (UserListConfig) adapter.getItem(i);
        //根据 每个 item的 状态  然后 做 相应1的 操作
        String a = ob.getTitle();

        switch (a) {
            case "企业圈"://1
                Log.e(TAG, "点击了企业圈");
                break;
            case "我的资产"://2
                Log.e(TAG, "点击了我的资产");
                MyAccountActivity.jump(getActivity());
                break;
            case "我的福利"://3
                Log.e(TAG, "点击了我的福利");
                MyBenefitsActivity.jump(getActivity());
                break;
            case "地址管理"://4
                Log.e(TAG, "点击了地址管理");
                AddressManageActivity.jump(getActivity());
                break;
            case "意见和反馈"://5
                Log.e(TAG, "点击了意见和反馈");
                AdviceActivity.jump(getActivity());
                break;
            case "关于礼舍"://6
                Log.e(TAG, "点击了关于礼舍");
//                AboutActivity.jump(getActivity(), getString(R.string.about_lishe), Globals.URL_ABOUT_LISHE);
                AboutUpdateActivity.jump(getActivity());
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.all_type_orders:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_ALL);
                break;
            case R.id.money:
                RechargeIntegralActivity.jump(getActivity());
                break;
            case R.id.order_wait_pay:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_WAIT_PAY);
                break;
            case R.id.order_wait_post:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_WAIT_SEND);
                break;
            case R.id.order_wait_recieved:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_WAIT_RECEIVE);
                break;
            case R.id.order_complete:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_OVER);
                break;
            case R.id.order_back:
                MyOrdersActivity.jump(getActivity(), Globals.ORDER_TYPE_COME_BACK);
                break;
            case R.id.setting:
                ActivitySetting.jump(getActivity());
                break;
            case R.id.show_gift://晒福利
                BusinessActivity.jump(getActivity());
                break;
            default:
                break;

        }
    }

    private void setUserInfo(UserInfoObject user) {
        if (!TextUtils.isEmpty(user.getName())) {
            username.setText(user.getName());
            ninhao.setText(",您好");
        } else {
            ninhao.setText("您好");
        }
        companyName = user.getComName();
        totalPoints.setText(user.getBalance());
        commonPoints.setText(user.getCommonAmount());
        limitPoints.setText(user.getLimitAmount());
        if (!TextUtils.isEmpty(user.getUserAvatarPath()))
            new ImageLoaderUtils().displayImage(user.getUserAvatarPath(), headIcon);
    }

    private void setOrderData(OrderType order) {
        if (order.getTitle().equals("待付款")) {
            setLabs(lab1, order);

        } else if (order.getTitle().equals("代发货")) {
            setLabs(lab2, order);

        } else if (order.getTitle().equals("待收货")) {
            setLabs(lab3, order);

        } else if (order.getTitle().equals("已完成")) {
            setLabs(lab4, order);

        } else if (order.getTitle().equals("退货/款")) {
            setLabs(lab5, order);

        }

    }

    private void setLabs(TextView lab, OrderType order) {
        if (Integer.parseInt(order.getNumber()) > 0) {
            lab.setVisibility(View.VISIBLE);
            lab.setText(order.getNumber());
        } else {
            lab.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!LSWebHelper.getInstance().isUserLogined()) {//未登录返回商城页
            ((MainActivity0) getActivity()).changePage(0);
        } else {
            LoadingUtils.createLoadingDialog(getActivity(), "加载中", true);
            HttpUserCenter.getUserInfoMain(null, response);
        }
    }

}
